<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Opening_model extends MY_Model {

    protected $opening;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "opening";
    }

    function get_openings($filters = array(),$counting = false){
        if (isset($this->CI->cache)){
            $caching = true;
            $cache_key = 'ci--OPENNING_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--OPENNING_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        }else{
            $caching = false;
        }

        $this->db->select('*');
        $this->db->from('opening');
        $param = array();
        if (isset($filters['opening_id'])){
            $this->db->where('opening_id',$filters['opening_id']);
        }

        if (isset($filters['name'])){
            $this->db->like('name',$filters['name']);
        }

        if (isset($filters['status'])){
            $this->db->where('status',$filters['status']);
        }

        if (isset($filters['alias'])){
            $this->db->where('alias',$filters['alias']);
        }

        if (isset($filters['time'])){
            $this->db->where('time',$filters['time']);
        }

        if (isset($filters['time_open_to'])){
            $this->db->where('start_time <=',$filters['time_open_to']);
            $this->db->where('end_time >=',$filters['time_open_to']);
        }

        if (isset($filters['time_open_from'])){
            $this->db->where('start_time <=',$filters['time_open_from']);
            $this->db->where('end_time >=',$filters['time_open_from']);
        }

        if (isset($filters['other_opening_id'])){
            $this->db->where('opening_id !=',$filters['other_opening_id']);
        }

        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $this->db->limit($filters['limit'], $offset);
        }

        $this->db->order_by('opening_id','DESC');
        $result = $this->db->get();

        if($counting !=false){
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $opening = array();
        foreach ($result->result_array() as $row ){
            $row['link'] = create_url('lich-khai-giang/'.$row['alias'],array('rewrite'=>true));
            $row['date'] = fdate($row['created'],"%A, %d/%m/%Y - %H:%M");
            $opening[] = $row;
        }
        if ($caching){
            $this->CI->cache->file->save($cache_key,$opening);
        }
        $result->free_result();

        return $opening;
    }

    function get_count($filter = array()){
        return $this->get_openings($filter,true);
    }

    function get_opening($openingId){
        if (!isset($this->opening[$openingId])){
            $opening = $this->get_openings(array('opening_id'=>$openingId));
            $this->opening[$openingId] = $opening;
        }else{
            $opening = $this->opening[$openingId];
        }
        if (empty($opening)) {
            return false;
        }
        return $opening[0];
    }

    function insert_opening($data){
        $_opening_id = $this->save($data);
        return $_opening_id;
    }

    function update_opening($data,$opening_id){
        return $this->update($attributes, array('opening_id' => $opening_id));
    }

    function delete_opening($opening_id) {
        return $this->delete(array('opening_id'=>$opening_id));
    }

    function change_status_opening($opening_id) {
        $status = $this->db->select('status')->from('opening')->where('opening_id', $opening_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('opening_id' => $opening_id));
    }

    function get_opening_by_alias($opening_alias){
        $opening = $this->get_openings(array('alias'=>$opening_alias),false);
        if ($opening){
            return $opening[0];
        }
        return false;
    }

}
?>